package org.particlethink.repository.acceptance;

import org.particlethink.entry.DeliverStatus;
import org.particlethink.entry.ObjectionType;
import org.particlethink.exception.NoPermissionException;
import org.particlethink.mapper.DeliverMapper;
import org.particlethink.mapper.ObjectionReasonMapper;
import org.particlethink.model.Deliver;
import org.particlethink.model.ObjectionReason;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Repository("acceptanceSupplierUserImpl")
public class SupplierUserImpl {//企业用户

    @Autowired
    public DeliverMapper deliverMapper;

    //确认验收结果
    public void confirm(int supplierId, int deliverID) {
        Deliver deliver = this.deliverMapper.selectByPrimaryKey(deliverID);
        if (!deliver.isSupplier(supplierId)) {
            throw new NoPermissionException();
        }

        deliver.setStatus(DeliverStatus.Warehoused);
        deliverMapper.updateByPrimaryKeySelective(deliver);
        // todo 判断是否所有物料都已出库完成，如果都已经出库完成则需要修改订单状态为完成
    }

    //异议验收结果
    public void objection(int supplierId, int deliverID, String objectionReason) {
        Deliver deliver = this.deliverMapper.selectByPrimaryKey(deliverID);
        if (!deliver.isSupplier(supplierId)) {
            throw new NoPermissionException();
        }
        deliver.setStatus(DeliverStatus.Objection);
        deliver.setRemark(objectionReason);

        deliverMapper.updateByPrimaryKeySelective(deliver);
    }
}
